/*
 * Copyright (c) 2017.
 * @author fff
 * 17-10-23 下午4:42
 *
 */

package com.cpit.qualitycontrol.service.impl;

import com.cpit.platform.common.constant.SystemConstant;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.common.utils.BeanUtil;
import com.cpit.platform.log.LogOperator;
import com.cpit.qualitycontrol.dao.CooperatorRepository;
import com.cpit.qualitycontrol.entity.Cooperator;
import com.cpit.qualitycontrol.service.CooperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class CooperatorServiceImpl implements CooperatorService{

    private final String loggerName = this.getClass().getName();

    @Autowired
    private CooperatorRepository rep = null;



    @Override
    public Cooperator findById(Long id) {
        return this.rep.findOne(id);
    }

    @Override
    public List<Cooperator> findAll() {
        return this.rep.findAll();
    }

    @Override
    public List<Cooperator> findByCondition(int page, int size, String order, String orderBy, String pattern) {
        if(orderBy == null || orderBy.length() == 0) {
            orderBy = "id";
        }
        Sort.Direction der = order.equals(SystemConstant.DIRECTION_ASC) ? Sort.Direction.ASC : Sort.Direction.DESC;
        PageRequest request = new PageRequest(page - 1, size, new Sort(der, orderBy));
        Page<Cooperator> pg;
        if(pattern == null || "".equals(pattern)) {
            pg = this.rep.findAll(request);
        } else {
            pg = this.rep.findByNameContaining(pattern, request);
        }
        return pg.getContent();
    }

    @Override
    public Cooperator create(Cooperator cooperator) throws Exception {
        if(cooperator == null) {
            throw new Exception("参数为空");
        }
        if(cooperator.getId() != null && cooperator.getId()>0) {
            LogOperator.info(loggerName, "合作方ID已赋值");
            cooperator.setId(0L);
        }
        this.checkUniqueConstraint(cooperator);
        Cooperator coop = rep.findByName(cooperator.getName());
        if(coop != null) {
            LogOperator.err(loggerName,"同名合作方已存在:" + cooperator.getName());
            throw new Exception("同名合作方已存在");
        }
        return this.rep.save(cooperator);
    }

    @Override
    public Cooperator modify(Cooperator cooperator) throws Exception {
        if(cooperator == null) {
            throw new Exception("参数为空");
        }
        Cooperator coop = rep.findOne(cooperator.getId());
        if(coop == null) {
            LogOperator.info(loggerName, "修改的合作方不存在:" + cooperator);
            throw new Exception("修改的合作方不存在");
        }
        this.checkUniqueConstraint(cooperator);

        return this.rep.save(cooperator);
    }

    @Override
    public void delete(Cooperator cooperator) throws Exception {
        if(cooperator == null) {
            throw new Exception("参数为空");
        }
        this.delete(cooperator.getId());
    }

    @Override
    public void delete(Long id) throws Exception {
        Cooperator cooperator = rep.findOne(id);
        if(cooperator == null) {
            LogOperator.info(loggerName, "删除的合作方不存在:" + id);
            throw new Exception("删除的合作方不存在");
        }
       rep.delete(id);
      
    }

    @Override
    public TableJsonData findByConditionJqg(TableDataQueryCondition cond) {
        PageRequest request = ServiceUtils.getPageRequestByTableDataQueryCondition(cond);

        String pattern = BeanUtil.getStringIdentifyNull(cond.getQueryCond().get("pattern"));
        Page<Cooperator> ret;
        if(pattern == null || "".equals(pattern)) {
            ret = this.rep.findAll(request);
        } else {
            ret = this.rep.findByNameContaining(pattern, request);
        }
        return new TableJsonData(cond, ret.getTotalElements(), ret.getContent());
    }

    private void checkUniqueConstraint(Cooperator cooperator) throws  Exception {
        Cooperator coop = rep.findByName(cooperator.getName());
        if(coop != null && !coop.getId().equals(cooperator.getId())) {
            LogOperator.err(loggerName,"同名合作方已存在:" + cooperator.getName());
            throw new Exception("同名合作方已存在");
        }
    }
}
